<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

  <script type="text/javascript">
  $(document).ready(function(){
	   $("#accordion").accordion();
		resultRe();
	
	   function resultRe(){
		   				
				if($("#flag").val()=="true"){
					$("#beforeReBtn").show();
				}else{
					$("#beforeReBtn").hide();
				}
				if($("#next").val()=="true"){
					$("#nextReBtn").show();
				}else{
					$("#nextReBtn").hide();
				}
				if($("#flag2").val()=="true"){
					$("#beforeSdBtn").show();
				}else{
					$("#beforeSdBtn").hide();
				}
				if($("#next2").val()=="true"){
					$("#nextSdBtn").show();
				}else{
					$("#nextSdBtn").hide();
				}
	   }//resultRe
	   
	   $("#searchBySenderBtn").click(function(){
		   if($("#searchBySender").val()==""){
			   alert("보낸사람 id를 입력해주세요");
			   return false;
		   }	   
		   });
	   
	   $("#searchByReceiverBtn").click(function(){
		   if($("#searchByReceiver").val()==""){
			   alert("받은사람 id를 입력해주세요");
			   return false;
		   }	   
		   });//받은사람으로 찾기
		
		   
/////////////////////아래는 받은 쪽지함 로직//////////////////////////////		   
		   
		     $("#beforeReBtn").click(function(){
		    	 exePagingReceive($("#reHd1").val());
		   });//받은 메세지함 이전 눌렀을 때
		   
		   $("#nextReBtn").click(function(){
			   exePagingReceive($("#reHd2").val());
		   });//받은 메세지함 다음 눌렀을 때
	   
		   //받은메세지 페이징 숫자 누르면 여기로
	   $(".pagingReceive").click(function(){
				exePagingReceive($(this).text());
	   }); //받은메세지 페이징 숫자 누르면
	  
	   function receivedPaging(){
		   $(".pagingReceive").click(function(){
			  var pr=$(this).text();
			  exePagingReceive(pr);
		   });//pagingSend 링크 클릭했을 때
	   }//sendedPaging function
		   
		  function exePagingReceive(pr){
			   $.ajax({
					  type:"get",
					  dataType:"json",
					  url:"${initParam.root }message.do",
					  data:"command=messageBox3&pageRe="+pr+"&pageSd=1",
					  success:function(result){
						  
						  var newInfo="";
						  $("#reHd2").val(result.info.receivedList.pagingBean.endPageOfPageGroup+1);
						  $("#reHd1").val(result.info.receivedList.pagingBean.startPageOfPageGroup-1);
						  $("#re1").val(result.info.receivedList.pagingBean.startPageOfPageGroup);
						  $("#re2").val(result.info.receivedList.pagingBean.endPageOfPageGroup);
						  
						  $.each(result.info.receivedList.list,function(index,sh){
							 newInfo+="<tr><td>"+sh.senderId+"</td>";
							 newInfo+="<td width='200'>"+sh.send_date+"</td>";
							 newInfo+="<td width='350'><a href='${initParam.root}message.do?command=viewOneMessage&no="+sh.no+"&nowrp="+result.info.receivedList.pagingBean.nowPage+"&nowsp="+result.info.sendedList.pagingBean.nowPage+"'>"+sh.content+"</a></td>";
							 newInfo+="<td>"+sh.didRead+"</td></tr>";
					  });
						  $("#receivedListBody").html(newInfo);
						   $("#flag").val(result.info.receivedList.pagingBean.previousPageGroup);
							$("#next").val(result.info.receivedList.pagingBean.nextPageGroup);
						  resultRe();
					
						  var startPageRe=$("#re1").val();
						  var endPageRe=$("#re2").val();
						  
						  var str="";
						  
						  for(i=parseInt(startPageRe);i<=parseInt(endPageRe);i++){
							//지금 페이지가 내가 있는 페이지면 링크 안걸리게
							  if(i==result.info.receivedList.pagingBean.nowPage){
								str+=i+" ";
							}else{
								str+="<a href='#' class='pagingReceive'>"+i+"</a> ";
							}  
						  }//for  
						$("#indexSpace").html(str);
						receivedPaging();
					  }
				   });  //ajax
		   }//exePagingReceive
		 

	   
///////////////////////아래부터 보낸메세지 로직////////////////////////////////

	   
	   $("#beforeSdBtn").click(function(){
		   exePagingSend($("#sdHd1").val());
	   });//보낸 메세지 이전 눌렀을 때
	   
	   $("#nextSdBtn").click(function(){
		  var nextSdBtn=$("#sdHd2").val();
		   exePagingSend(nextSdBtn);
	   });//보낸 메세지 다음 눌렀을 때
		   
	   //보낸메세지 페이징 숫자 누르면 여기로
	    $(".pagingSend").click(function(){
		   		var ps1=$(this).text();	
		   		exePagingSend(ps1);
	   }); 
   
	   function sendedPaging(){
		   $(".pagingSend").click(function(){
			  var ps=$(this).text();
			   exePagingSend(ps);
		   });//pagingSend 링크 클릭했을 때
	   }//sendedPaging function
	   
	   function exePagingSend(ps){
		   $.ajax({
			  type:"get",
			  dataType:"json",
			  url:"${initParam.root }message.do",
			  data:"command=messageBox3&pageSd="+ps+"&pageRe=1",
			  success:function(result){
				  var newInfo="";
				  $("#sdHd1").val(result.info.sendedList.pagingBean.startPageOfPageGroup-1);
				  $("#sdHd2").val(result.info.sendedList.pagingBean.endPageOfPageGroup+1);
				  $("#sd1").val(result.info.sendedList.pagingBean.startPageOfPageGroup);
				  $("#sd2").val(result.info.sendedList.pagingBean.endPageOfPageGroup);
				  $.each(result.info.sendedList.list,function(index,sh){
					 newInfo+="<tr><td>"+sh.receiverId+"</td>";
					 newInfo+="<td width='200'>"+sh.send_date+"</td>";
					 newInfo+="<td width='350'><a href='${initParam.root}message.do?command=viewOneMessage&no="+sh.no+"&nowrp="+result.info.receivedList.pagingBean.nowPage+"&nowsp="+result.info.sendedList.pagingBean.nowPage+"'>"+sh.content+"</a></td>";
					newInfo+="<td>"+sh.didRead+"</td></tr>";
				  });
				 $("#sendedListBody").html(newInfo);
				 $("#flag2").val(result.info.sendedList.pagingBean.previousPageGroup);
				$("#next2").val(result.info.sendedList.pagingBean.nextPageGroup);
				resultRe();
				
				 var startPageSd=$("#sd1").val();
				  var endPageSd=$("#sd2").val();
				  //alert(startPageSd+" "+endPageSd);
				  var str2="";
				  for(i=parseInt(startPageSd);i<=parseInt(endPageSd);i++){				 
					//지금 페이지가 내가 있는 페이지면 링크 안걸리게
					  if(i==result.info.sendedList.pagingBean.nowPage){
						str2+=i+" ";
					}else{
						str2+="<a href='#' class='pagingSend'>"+i+"</a> ";
					}
				  }
				$("#indexSpace2").html(str2);	
				 sendedPaging();
			  }
		   });
	   }
	   
	  });
  </script>
  <style type="text/css">
	#accordion{
		width: 750px
	}
</style>

	<form id="newMessageForm" action="message.do" method="post">
	<input type="submit" value="새로운 메세지 보내기">
	<input type="hidden" name="command" value="goNewMessage">
	</form>	
	
	<input type="hidden" id="reHd1" value="${requestScope.info.receivedList.pagingBean.startPageOfPageGroup-1 }">
	<input type="hidden" id="reHd2" value="${requestScope.info.receivedList.pagingBean.endPageOfPageGroup+1 }">
	<input type="hidden" id="sdHd1" value="${requestScope.info.sendedList.pagingBean.startPageOfPageGroup-1}">
	<input type="hidden" id="sdHd2" value="${requestScope.info.sendedList.pagingBean.endPageOfPageGroup+1}">
	
<br>
	<div id="accordion">
<h3>받은메세지(${requestScope.info.unreadCount })</h3>
<div>

<table border="1">
	<tr>
		<th>보낸사람</th><th>보낸시간</th><th>내용</th><th>내가 읽었나?</th>
	</tr>
	
<tbody id="receivedListBody">
	<c:forEach items="${requestScope.info.receivedList.list }" var="list">
	<tr>
		<td>${list.senderId }</td>
		<td width="200">${list.send_date }</td>
		<td width="350"><a href="${initParam.root}message.do?command=viewOneMessage&no=${list.no}&nowrp=${requestScope.info.receivedList.pagingBean.nowPage }&nowsp=${requestScope.info.sendedList.pagingBean.nowPage }">${list.content }</a></td>
		<td>${list.didRead }</td>
	</tr>
	</c:forEach>	
</tbody>
	
	</table>
	
	
<input type="hidden" name="flag" id="flag" value="${requestScope.info.receivedList.pagingBean.previousPageGroup }">
<input type="hidden" name="next" id="next" value="${requestScope.info.receivedList.pagingBean.nextPageGroup }">

<center>

	<input type="button" id="beforeReBtn" value="이전">

 <input type="hidden" id="re1" value="${requestScope.info.receivedList.pagingBean.startPageOfPageGroup }">
  <input type="hidden" id="re2" value="${requestScope.info.receivedList.pagingBean.endPageOfPageGroup }">


<input type="hidden" id="nowPage" value="${requestScope.info.receivedList.pagingBean.nowPage }">


  <span id="indexSpace">
 
  <c:forEach var="i" begin="${requestScope.info.receivedList.pagingBean.startPageOfPageGroup }" end="${requestScope.info.receivedList.pagingBean.endPageOfPageGroup }">
		<c:choose>
			<c:when test="${i == requestScope.info.receivedList.pagingBean.nowPage }">
				${i }
			</c:when>
			<c:otherwise>
				 <a href="#" class="pagingReceive">${i}</a>
			</c:otherwise>
		</c:choose>
</c:forEach>

</span>
 

<input type="button" id="nextReBtn" value="다음">
</center>
<br>
<div align="right">
<form action="${initParam.root }message.do" method="post">
	보낸사람으로 검색: <input type="text" id="searchBySender" name="searchBySender">
	<input type="submit" value="검색" id="searchBySenderBtn" >
	<input type="hidden" name="command" value="searchBySender">
	<input type="hidden" name="searchPage" value="1">
</form>

</div>


</div>

<h3>보낸 메세지</h3>
<div>

	<div id="SdMessageBox">
		<table border="1">
		<tr>
			<th>받는사람</th><th>보낸시간</th><th>내용</th><th>받은사람이 읽었나?</th>
		</tr>
		<tbody id="sendedListBody">
		<c:forEach items="${requestScope.info.sendedList.list }" var="list2">
		<tr>
			<td>${list2.receiverId }</td>
			<td width="200">${list2.send_date }</td>
			<td width="350"><a href="${initParam.root}message.do?command=viewOneMessage&no=${list2.no}&nowrp=${requestScope.info.receivedList.pagingBean.nowPage }&nowsp=${requestScope.info.sendedList.pagingBean.nowPage }">${list2.content }</a></td>
			<td>${list2.didRead }</td>
		</tr>
		</c:forEach>	
		</tbody>
		</table>
	</div>

<input type="hidden" name="flag2" id="flag2" value="${requestScope.info.sendedList.pagingBean.previousPageGroup}">
<input type="hidden" name="next2" id="next2" value="${requestScope.info.sendedList.pagingBean.nextPageGroup }">

<hr>

 <input type="hidden" id="sd1" value="${requestScope.info.sendedList.pagingBean.startPageOfPageGroup }">
  <input type="hidden" id="sd2" value="${requestScope.info.sendedList.pagingBean.endPageOfPageGroup }">
<center>
<input type="button" id="beforeSdBtn" value="이전">

	<span id="indexSpace2">
	 <c:forEach var="i" begin="${requestScope.info.sendedList.pagingBean.startPageOfPageGroup }" end="${requestScope.info.sendedList.pagingBean.endPageOfPageGroup }">
		<c:choose>
			<c:when test="${i == requestScope.info.sendedList.pagingBean.nowPage }">
				${i }
			</c:when>
			<c:otherwise>
				 <a href="#" class="pagingSend">${i}</a>
			</c:otherwise>
		</c:choose>
</c:forEach>
</span>

	<input type="button" id="nextSdBtn" value="다음">
		</center>

<div align="right">
<form action="${initParam.root }message.do" method="post">
	받은사람으로 검색: <input type="text" id="searchByReceiver" name="searchByReceiver">
	<input type="submit" value="검색" id="searchByReceiverBtn" >
	<input type="hidden" name="command" value="searchByReceiver">
	<input type="hidden" name="searchPage" value="1">
</form>

	</div>
</div>